package com.qiwx.pigeon.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.qiwx.pigeon.R;


/**
 * @description
 * @Author qiwx
 * @Date 2020-03-22 13:27
 * 箭头View
 **/
public class ArrowView extends View {
    Paint paint;
    int arrowLen;//箭头长度
    int rectHeight;//矩形的高度
    int arrowColor;
    int direction;//方向

    public ArrowView(Context context) {
        this(context, null, 0);
    }

    public ArrowView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArrowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final float scale = context.getResources().getDisplayMetrics().density;

        // 获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArrowView);
        if (typedArray != null) {
            arrowLen = (int)typedArray.getDimension(R.styleable.ArrowView_arrowLen, 0);
            direction=typedArray.getInt(R.styleable.ArrowView_direction, 0);
            rectHeight = (int)typedArray.getDimension(R.styleable.ArrowView_rectHeight, 0);
            arrowColor = typedArray.getColor(R.styleable.ArrowView_arrowColor, context.getResources().getColor(R.color.arrow_bg));

        }
        if (arrowLen <= 0) {
            arrowLen = (int) (scale * 12 + 0.5f);
        }
        if (rectHeight <= 0) {
            rectHeight = (int) (scale * 2 + 0.5f);
        }
//        if (arrowColor < 0) {
//            arrowColor = context.getResources().getColor(R.color.arrow_bg);
//        }
        init(context);
    }

    private void init(Context context) {
        paint = new Paint();
        paint.setColor(arrowColor);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int startX = getWidth() / 2;
        int startY = getHeight() / 2;
        //画第一个圆
        canvas.drawCircle(startX + arrowLen / 2, startY + arrowLen / 2 - rectHeight / 2, rectHeight / 2, paint);
        //画箭头左边，即底部
        RectF rectF = new RectF(startX - arrowLen / 2, startY + arrowLen / 2 - rectHeight, startX + arrowLen / 2, startY + arrowLen / 2);
        canvas.drawRect(rectF, paint);
        //画箭头右边 ，即上部
        RectF rectF1 = new RectF(startX - arrowLen / 2, startY - arrowLen / 2, startX - arrowLen / 2 + rectHeight, +startY + arrowLen / 2);
        canvas.drawRect(rectF1, paint);
        //画圆
        canvas.drawCircle(startX - arrowLen / 2 + rectHeight / 2, startY - arrowLen / 2, rectHeight / 2, paint);

        if(direction!=0){
            setRotation(direction);
        }
    }

    //设置箭头大小
    public void setArrowSize(int arrowLen, int rectHeight) {
        this.arrowLen = arrowLen;
        this.rectHeight = rectHeight;
        invalidate();
    }

    //箭头向上
    public void changeArrowUp() {
        setRotation(135);
    }

    //箭头向右
    public void changeArrowRight() {
        setRotation(225);
    }

    //箭头向下
    public void changeArrowDown() {
        setRotation(-45);
    }

    //箭头向左
    public void changeArrowLeft() {
        setRotation(45);
    }
}
